package com.bm.greeting.config;


import com.bm.greeting.entity.EntertainmentNews;
import com.bm.greeting.entity.Weather;
import me.chanjar.weixin.mp.api.WxMpInMemoryConfigStorage;
import me.chanjar.weixin.mp.api.WxMpService;
import me.chanjar.weixin.mp.api.impl.WxMpServiceImpl;
import me.chanjar.weixin.mp.bean.template.WxMpTemplateData;
import me.chanjar.weixin.mp.bean.template.WxMpTemplateMessage;

import java.util.List;
import java.util.Map;

/**
 * @author Jinmy
 * @date 2022-08-04 21:09
 */
public class Pusher {

    private static String appId = "wxcf528986a14b95f0";
    private static String secret = "961980d1820519f3418c82085eabb604";

    /**
     * 不明UserId
     */
    private static String bmId = "oejpg57BP0icSa5PuOqqfFj1xAlY";
    private static String gzrId = "oejpg5xCGQ5d6xrdxfrXMVnLHhSc";
    /**
     * 小许UserId
     */
    private static String xyId = "oejpg54WGr56WuFV0Puex6jQBtXk";
    /**
     * 模板Id
     */
//    private static String TEMPLATE_ID = "vjxqZ9BNCLwmQceXC8S5fMYgs3xSzPmOZkWRjn2E_WQ";
//    private static String GOOD_MORNING_TEMPLATE_ID = "PtvhhhvU5_ngIQpJ9ku-DMJUANCy3R2suci4komd-Uc";
//    private static String GOOD_MORNING_TEMPLATE_ID = "ooaHkPYQSPnS6hlcgcZtgmWIEJnxLWtvtvaVMvycVuM";
    private static String GOOD_MORNING_TEMPLATE_ID = "jcjuMkpuYdN7oSMgWUKiLL1bY9rqaSKlcCB0B6QFsJk";
    private static String FEEDBACK_TEMPLATE_ID = "8mLLjH5fZ0lGTIcMtXeazEI1rYK-1yvzpcNveOUkFvU";


    public static void pushBaby() {
        //TODO 配置
        WxMpInMemoryConfigStorage wxStorage = new WxMpInMemoryConfigStorage();
        wxStorage.setAppId(appId);
        wxStorage.setSecret(secret);
        WxMpService wxMpService = new WxMpServiceImpl();
        wxMpService.setWxMpConfigStorage(wxStorage);
        //TODO 推送消息
        WxMpTemplateMessage templateMessage = templateMorning(xyId, GOOD_MORNING_TEMPLATE_ID);

        try {
            System.out.println(templateMessage.toJson());
            System.out.println(wxMpService.getTemplateMsgService().sendTemplateMsg(templateMessage));
        } catch (Exception e) {
            System.out.println("Push Error：" + e.getMessage());
            e.printStackTrace();
        }
    }

    public static void pushTest() {
        //TODO 配置
        WxMpInMemoryConfigStorage wxStorage = new WxMpInMemoryConfigStorage();
        wxStorage.setAppId(appId);
        wxStorage.setSecret(secret);
        WxMpService wxMpService = new WxMpServiceImpl();
        wxMpService.setWxMpConfigStorage(wxStorage);
        //TODO 推送消息
        WxMpTemplateMessage templateMessage = templateMorning(bmId, GOOD_MORNING_TEMPLATE_ID);

        try {
            System.out.println(templateMessage.toJson());
            System.out.println(wxMpService.getTemplateMsgService().sendTemplateMsg(templateMessage));
        } catch (Exception e) {
            System.out.println("Push Error：" + e.getMessage());
            e.printStackTrace();
        }
    }


    public static void pushGzr() {
        //TODO 配置
        WxMpInMemoryConfigStorage wxStorage = new WxMpInMemoryConfigStorage();
        wxStorage.setAppId(appId);
        wxStorage.setSecret(secret);
        WxMpService wxMpService = new WxMpServiceImpl();
        wxMpService.setWxMpConfigStorage(wxStorage);
        //TODO 推送消息
        WxMpTemplateMessage templateMessage = templateMorning(gzrId, GOOD_MORNING_TEMPLATE_ID);

        try {
            System.out.println(templateMessage.toJson());
            System.out.println(wxMpService.getTemplateMsgService().sendTemplateMsg(templateMessage));
        } catch (Exception e) {
            System.out.println("Push Error：" + e.getMessage());
            e.printStackTrace();
        }
    }

    public static void returnBm(String name, String isSuccess) {
        //TODO 配置
        WxMpInMemoryConfigStorage wxStorage = new WxMpInMemoryConfigStorage();
        wxStorage.setAppId(appId);
        wxStorage.setSecret(secret);
        WxMpService wxMpService = new WxMpServiceImpl();
        wxMpService.setWxMpConfigStorage(wxStorage);
        //TODO 推送消息
        WxMpTemplateMessage templateMessage = templateFeedback(bmId, FEEDBACK_TEMPLATE_ID, name, isSuccess);

        try {
            System.out.println(templateMessage.toJson());
            System.out.println(wxMpService.getTemplateMsgService().sendTemplateMsg(templateMessage));
        } catch (Exception e) {
            System.out.println("Push Error：" + e.getMessage());
            e.printStackTrace();
        }
    }

    public static void pushNews() {
        //TODO 配置
        WxMpInMemoryConfigStorage wxStorage = new WxMpInMemoryConfigStorage();
        wxStorage.setAppId(appId);
        wxStorage.setSecret(secret);
        WxMpService wxMpService = new WxMpServiceImpl();
        wxMpService.setWxMpConfigStorage(wxStorage);
        //TODO 推送消息
        WxMpTemplateMessage templateMessage = templateNews(bmId, "iJmQeB3N7kbSb-9hXdzJKkjZguTm2su0hKggSQCBnAk");

        try {
            System.out.println(templateMessage.toJson());
            System.out.println(wxMpService.getTemplateMsgService().sendTemplateMsg(templateMessage));
        } catch (Exception e) {
            System.out.println("Push Error：" + e.getMessage());
            e.printStackTrace();
        }
    }

    private static WxMpTemplateMessage templateMorning(String UserId, String templateId) {
        WxMpTemplateMessage templateMessage = WxMpTemplateMessage.builder()
                .toUser(UserId)
                .templateId(templateId)
                .build();
        Weather weather = ToolsUtils.getWeather();
        Map<String, String> yiTyjmap = ToolsUtils.getEnsentence();
        Map<String, String> qingShiMap = ToolsUtils.getQingShi();

        templateMessage.addData(new WxMpTemplateData("riqi", weather.getDate() + "  " + weather.getWeek(), "#00BFFF"));
        templateMessage.addData(new WxMpTemplateData("tianqi", weather.getText_now(), "#00FFFF"));
        templateMessage.addData(new WxMpTemplateData("low", weather.getLow() + "", "#173177"));
        templateMessage.addData(new WxMpTemplateData("temp", weather.getTemp() + "", "#EE212D"));
        templateMessage.addData(new WxMpTemplateData("high", weather.getHigh() + "", "#FF6347"));
        templateMessage.addData(new WxMpTemplateData("windclass", weather.getWind_class() + "", "#42B857"));
        templateMessage.addData(new WxMpTemplateData("winddir", weather.getWind_dir() + "", "#B95EA3"));
        templateMessage.addData(new WxMpTemplateData("caihongpi", ToolsUtils.getCaiHongPi(), "#FF69B4"));
        templateMessage.addData(new WxMpTemplateData("lianai", JiNianRiUtils.getLianAi() + "", "#FF1493"));
        templateMessage.addData(new WxMpTemplateData("shengri1", JiNianRiUtils.getBirthday_Jimmy() + "", "#FFA500"));
        templateMessage.addData(new WxMpTemplateData("shengri2", JiNianRiUtils.getBirthday_xy() + "", "#FFA500"));
        templateMessage.addData(new WxMpTemplateData("en", yiTyjmap.get("en") + "", "#C71585"));
        templateMessage.addData(new WxMpTemplateData("zh", yiTyjmap.get("zh") + "", "#C71585"));
        templateMessage.addData(new WxMpTemplateData("shxqm", ToolsUtils.getTheTrickOfLife() + "", "#F03752"));
        templateMessage.addData(new WxMpTemplateData("QScontent", qingShiMap.get("content") + "", "#158BB8"));
        templateMessage.addData(new WxMpTemplateData("QSsource", qingShiMap.get("source") + "", "#2775B6"));
        templateMessage.addData(new WxMpTemplateData("QSauthor", qingShiMap.get("author") + "", "#2775B6"));
        templateMessage.addData(new WxMpTemplateData("zaoan", ToolsUtils.getGoodMorning() + "", "#DE3F7C"));


        String beizhu = "❤许小仙女";
        if (JiNianRiUtils.getLianAi() % 365 == 0) {
            beizhu = "今天是恋爱" + (JiNianRiUtils.getLianAi() / 365) + "周年纪念日！";
        }
        if (JiNianRiUtils.getBirthday_Jimmy() == 0) {
            beizhu = "今天是bm生日，生日快乐呀！";
        }
        if (JiNianRiUtils.getBirthday_xy() == 0) {
            beizhu = "今天是宝贝生日，生日快乐呀！";
        }
        templateMessage.addData(new WxMpTemplateData("beizhu", beizhu, "#FF0000"));
        return templateMessage;
    }

    private static WxMpTemplateMessage templateFeedback(String UserId, String templateId, String name, String isSuccess) {
        WxMpTemplateMessage templateMessage = WxMpTemplateMessage.builder()
                .toUser(UserId)
                .templateId(templateId)
                .build();

        templateMessage.addData(new WxMpTemplateData("name", name, "#00BFFF"));
        templateMessage.addData(new WxMpTemplateData("IsSuccessful", isSuccess, "#00BFFF"));

        return templateMessage;
    }

    private static WxMpTemplateMessage templateNews(String UserId, String templateId) {
        WxMpTemplateMessage templateMessage = WxMpTemplateMessage.builder()
                .toUser(UserId)
                .templateId(templateId)
                .build();
        List<EntertainmentNews> entertainmentNews = ToolsUtils.getNews();
        templateMessage.addData(new WxMpTemplateData("title", entertainmentNews.get(0).getTitle() + "", "#00BFFF"));
        templateMessage.addData(new WxMpTemplateData("ctime", entertainmentNews.get(0).getCtime() + "", "#00BFFF"));
        templateMessage.addData(new WxMpTemplateData("picUrl", entertainmentNews.get(0).getPicUrl() + "", "#00BFFF"));
        templateMessage.addData(new WxMpTemplateData("url", entertainmentNews.get(0).getUrl() + "", "#00BFFF"));
        return templateMessage;
    }

}
